﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Data;


namespace ConcreteMappersAndObjects.Mappers
{
    public class TCCUDataMapper : AbstractDataMapper<TeacherClassCUnitKey, TeacherClassCUnit>
    {
        private IEntityMapper<int, Teacher> _teacherMapper;
        private IEntityMapper<int, CUnit> _cunitMapper;
        private IEntityMapper<int, SClass> _classMapper;

        public void SetTeacherMapper(IEntityMapper<int, Teacher> mapper) { _teacherMapper = mapper; }
        public void SetCUnitsMapper(IEntityMapper<int, CUnit> mapper) { _cunitMapper = mapper; }
        public void SetClassesMapper(IEntityMapper<int, SClass> mapper) { _classMapper = mapper; }
        
        protected override TeacherClassCUnitKey GetKeyFrom(System.Data.SqlClient.SqlDataReader sdr)
        {   //teacher, class, cunit
            return new TeacherClassCUnitKey(sdr.GetInt32(0), sdr.GetInt32(1), sdr.GetInt32(2)); 
        }

        protected override string DoGetAllStatement()
        {
            return "select TeacherID, ClassID, CUnitID from TeacherClassCUnit";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " where TeacherID = @TeacherID and ClassID = @ClassID and CUni.tID = @CUnitID";
        }

        protected override void BindFindStatement(System.Data.SqlClient.SqlCommand cmd, TeacherClassCUnitKey key)
        {
            cmd.Parameters.AddWithValue("@TeacherID", key.TeacherKey);
            cmd.Parameters.AddWithValue("@ClassID", key.ClassKey);
            cmd.Parameters.AddWithValue("@CUnitID", key.CUnitKey);
        }

        protected override TeacherClassCUnit DoLoad(System.Data.SqlClient.SqlDataReader sdr)
        {
            TeacherClassCUnitKey key = GetKeyFrom(sdr);
            ConcreteValueHolder<int, Teacher> teacher = new ConcreteValueHolder<int,Teacher>(key.TeacherKey, _teacherMapper);
            ConcreteValueHolder<int, CUnit> cunit = new ConcreteValueHolder<int,CUnit>(key.CUnitKey, _cunitMapper);
            ConcreteValueHolder<int, SClass> sclass = new ConcreteValueHolder<int, SClass>(key.ClassKey, _classMapper);

            return new TeacherClassCUnit(key, teacher, cunit, sclass);
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(System.Data.SqlClient.SqlCommand cmd, TeacherClassCUnit t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "insert into TeacherClassCUnit (TeacherID, ClassID, CUnitID) values (@TeacherID, @ClassID, @CUnitID)";
        }

        protected override void DoBindInsert(System.Data.SqlClient.SqlCommand cmd, TeacherClassCUnit t)
        {
            cmd.Parameters.Add("@TeacherID", SqlDbType.Int).Value = t.Teacher.ID;
            cmd.Parameters.Add("@ClassID", SqlDbType.Int).Value = t.Class.ID;
            cmd.Parameters.Add("@CUnitID", SqlDbType.Int).Value = t.CUnit.ID;
        }

        protected override string GetRemoveCommand()
        {
            return "delete from TeacherClassCUnit where TeacherID = @TeacherID and ClassID = @ClassID and CUnitID = @CUnitID";
        }

        protected override void DoBindRemove(System.Data.SqlClient.SqlCommand cmd, TeacherClassCUnit t)
        {
            cmd.Parameters.Add("@TeacherID", SqlDbType.Int).Value = t.Teacher.ID;
            cmd.Parameters.Add("@ClassID", SqlDbType.Int).Value = t.Class.ID;
            cmd.Parameters.Add("@CUnitID", SqlDbType.Int).Value = t.CUnit.ID;
        }

        protected override void FinishUpdateObject(TeacherClassCUnit t)
        {
            
        }

        protected override void FinishNewObject(TeacherClassCUnit t, object id)
        {
            t.ID = new TeacherClassCUnitKey(t.Teacher.ID, t.Class.ID, t.CUnit.ID);
        }

        protected override void FinishDeleteObject(TeacherClassCUnit t)
        {
       
        }
    }
}
